package control;

import Entity.goodBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

@WebServlet("/deleteUserServlet")
public class deleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String use_id = (String)request.getParameter("use_id");
        System.out.println(use_id);
        try{
            Connection con=null;
            Statement stmt=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            con= DriverManager.getConnection(url,"root","root");
            HttpSession session=request.getSession();
            stmt=con.createStatement();
            String sql="DELETE FROM `shop_user` WHERE use_id = '"+use_id+"'";
            int num;
            num = stmt.executeUpdate(sql);
            System.out.println(num+"条删除成功！");
            request.getSession().removeAttribute("userslist");
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        response.sendRedirect("http://localhost:8080/user_manage.jsp");
        return;
    }
}
